package com.liveinstars.im.core.service.impl;

import com.baidu.unbiz.easymapper.MapperFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.liveinstars.api.dto.BatchImGetUserReq;
import com.liveinstars.api.dto.ImGetUserInfo;
import com.liveinstars.api.facade.ImUserCentreApi;
import com.liveinstars.im.domain.dao.model.Fans;
import com.liveinstars.im.domain.manager.FansManager;
import com.liveinstars.api.im.vo.FansVO;
import com.liveinstars.api.im.vo.UserFansInfoVO;
import com.liveinstars.im.core.service.FansService;
import com.onepiece.shipelves.common.Result;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhouze
 * @date: 2020/3/31
 */
@Service("fansService")
public class FansServiceImpl implements FansService {

    @Autowired
    private FansManager fansManager;

    @Autowired
    private ImUserCentreApi imUserCentreApi;

    @Override
    public List<FansVO> listFansByUserId(Long userId) {
        List<FansVO> list = Lists.newArrayList();
        List<Fans> fansList = fansManager.listByUserId(userId);
        if (CollectionUtils.isNotEmpty(fansList)) {
            List<Long> userIdList = fansList.stream().map(Fans::getFansId).collect(Collectors.toList());
            BatchImGetUserReq req = new BatchImGetUserReq();
            req.setUids(userIdList);
            Result<List<ImGetUserInfo>> listResult = imUserCentreApi.batchImGetUserInfo(req);
            Map<Long, ImGetUserInfo> userInfoMap = Maps.newHashMap();
            if (listResult.getCode().equals(Result.SUCCESS)) {
                listResult.getData().forEach(user -> userInfoMap.put(user.getUid(), user));
            }
            list = fansList.stream().map(fans -> {
                FansVO fansVO = new FansVO();
                if (userInfoMap.containsKey(fans.getFansId())) {
                    ImGetUserInfo userInfo = userInfoMap.get(fans.getFansId());
                    fansVO.setDate(userInfo.getRegdate().toString());
                    fansVO.setGroupId(userInfo.getGroupId().toString());
                    fansVO.setName(userInfo.getUserName());
                    fansVO.setLogo(userInfo.getLogo());
                }
                fansVO.setFansId(fans.getFansId());
                fansVO.setUserId(fans.getUserId());
                return fansVO;
            }).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<FansVO> listFollowByUserId(Long userId) {
        List<FansVO> list = Lists.newArrayList();
        List<Fans> fansList = fansManager.listByFansId(userId);
        if (CollectionUtils.isNotEmpty(fansList)) {
            List<Long> userIdList = fansList.stream().map(Fans::getUserId).collect(Collectors.toList());
            BatchImGetUserReq req = new BatchImGetUserReq();
            req.setUids(userIdList);
            Result<List<ImGetUserInfo>> listResult = imUserCentreApi.batchImGetUserInfo(req);
            Map<Long, ImGetUserInfo> userInfoMap = Maps.newHashMap();
            if (listResult.getCode().equals(Result.SUCCESS)) {
                listResult.getData().forEach(user -> userInfoMap.put(user.getUid(), user));
            }
            list = fansList.stream().map(fans -> {
                FansVO fansVO = new FansVO();
                if (userInfoMap.containsKey(fans.getFansId())) {
                    ImGetUserInfo userInfo = userInfoMap.get(fans.getFansId());
                    fansVO.setDate(userInfo.getRegdate().toString());
                    fansVO.setGroupId(userInfo.getGroupId().toString());
                    fansVO.setName(userInfo.getUserName());
                    fansVO.setLogo(userInfo.getLogo());
                }
                fansVO.setFansId(fans.getFansId());
                fansVO.setUserId(fans.getUserId());
                return fansVO;
            }).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public FansVO getFansByUserIdAndFansId(Long userId, Long fansId) {
        Fans fans = fansManager.getByUserIdAndFansId(userId, fansId);
        if (fans != null) {
            FansVO fansVO = MapperFactory.getCopyByRefMapper().mapClass(Fans.class, FansVO.class).register()
                    .map(fans, FansVO.class);
            return fansVO;
        }
        return null;
    }

    @Override
    public boolean isFans(Long userId, Long fansId) {
        Fans fans = fansManager.getByUserIdAndFansId(userId, fansId);
        if (fans != null) {
            return true;
        }
        return false;
    }

    @Override
    public void addFans(FansVO fansVO) {
        Fans fans = new Fans();
        fans.setFansId(fansVO.getFansId());
        fans.setUserId(fansVO.getUserId());
        fansManager.add(fans);
    }

    @Override
    public UserFansInfoVO getUserFansInfo(Long userId) {
        UserFansInfoVO userFansInfoVO = new UserFansInfoVO();
        Integer userFollowNum = fansManager.countByFansId(userId);
        Integer userFansNum = fansManager.countByUserId(userId);
        userFansInfoVO.setMyFansCount(userFansNum);
        userFansInfoVO.setMyConcernCount(userFollowNum);
        return userFansInfoVO;
    }
}
